package com.ruoyi.ejkgl.grsxgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ejkgl.grsxgl.domain.Grsxjl;
import com.ruoyi.ejkgl.grsxgl.service.IGrsxjlService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 个人事项记录Controller
 *
 * @author huacai
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/ejkgl/grsxjl")
public class GrsxjlController extends BaseController {
    @Resource
    private IGrsxjlService grsxjlService;

    /**
     * 查询个人事项记录列表
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Grsxjl grsxjl) {
        startPage();
        List<Grsxjl> list = grsxjlService.selectGrsxjlList(grsxjl);
        return getDataTable(list);
    }

    /**
     * 导出个人事项记录列表
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:export')")
    @Log(title = "个人事项记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Grsxjl grsxjl) {
        List<Grsxjl> list = grsxjlService.selectGrsxjlList(grsxjl);
        ExcelUtil<Grsxjl> util = new ExcelUtil<Grsxjl>(Grsxjl.class);
        util.exportExcel(response, list, "个人事项记录数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Grsxjl> util = new ExcelUtil<Grsxjl>(Grsxjl.class);
        util.importTemplateExcel(response, "个人事项记录数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "个人事项记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Grsxjl> util = new ExcelUtil<Grsxjl>(Grsxjl.class);
        InputStream inputStream = file.getInputStream();
        List<Grsxjl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = grsxjlService.batchInsertGrsxjl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取个人事项记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:query')")
    @GetMapping(value = "/{grsxjlId}")
    public AjaxResult getInfo(@PathVariable("grsxjlId") String grsxjlId) {
        return success(grsxjlService.selectGrsxjlByGrsxjlId(grsxjlId));
    }

    /**
     * 新增个人事项记录
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:add')")
    @Log(title = "个人事项记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Grsxjl grsxjl) {
        grsxjl.setGrsxjlId(IdUtils.fastSimpleUUID());
        grsxjl.setUserId(Math.toIntExact(getUserId())); //用户ID
        grsxjl.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        grsxjl.setCreateBy(getUsername()); //创建人
        grsxjl.setIdWhere("存在");
        return toAjax(grsxjlService.insertGrsxjl(grsxjl));
    }

    /**
     * 修改个人事项记录
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:edit')")
    @Log(title = "个人事项记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Grsxjl grsxjl) {
        return toAjax(grsxjlService.updateGrsxjl(grsxjl));
    }

    /**
     * 删除个人事项记录
     */
    @PreAuthorize("@ss.hasPermi('ejkgl:grsxjl:remove')")
    @Log(title = "个人事项记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{grsxjlIds}")
    public AjaxResult remove(@PathVariable String[] grsxjlIds) {
        return toAjax(grsxjlService.deleteGrsxjlByGrsxjlIds(grsxjlIds));
    }
}
